package com.kehao.leetcode.middle.sort_search;

import org.junit.Test;

public class ColorSolution {
    public void sortColors(int[] nums) {
        int leftPos =0,rightPos = nums.length-1;
        for (int i = 0; i <= rightPos; i++) {
            if(nums[i]==0){
                swap(nums,i,leftPos);
                leftPos++;
            }else if(nums[i]==2){
                swap(nums,i,rightPos);
                rightPos--;
                i--;
            }
        }
    }
    
    private void swap(int[]nums,int pos1,int pos2){
        int temp = nums[pos1];
        nums[pos1] = nums[pos2];
        nums[pos2] = temp;
    }


    @Test
    public void test01(){
        int[] nums = {2,0,2,1,1,0};
        sortColors(nums);
        for (int num : nums) {
            System.out.print(num);
        }
    }

    @Test
    public void test02(){
        int[] nums = {2,0,1};
        sortColors(nums);
        for (int num : nums) {
            System.out.print(num);
        }
    }

    @Test
    public void test03(){
        int[] nums = {1,2,0};
        sortColors(nums);
        for (int num : nums) {
            System.out.print(num);
        }
    }
}
